package com.wobangkj.common.service.impl;

import com.wobangkj.common.model.crud.Dynamic;
import com.wobangkj.common.repository.DynamicRepository;
import com.wobangkj.tool.api.result.Result;
import com.wobangkj.tool.util.sql.CrudUtil;
import lombok.extern.slf4j.Slf4j;
import com.wobangkj.common.service.DynamicService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.transaction.Transactional;
import java.util.List;
import java.util.Map;

import static com.wobangkj.tool.util.RestUtil.copyNonNullProperties;

@Service
@Slf4j
public class DynamicImpl implements DynamicService{
    @Autowired
    private DynamicRepository repository;

    @Override
    public Object search(Map<String, Object> params, Dynamic data) {

        return CrudUtil.search(params, data, repository);
    }

    @Override
    public Object id(Long id) {
        return CrudUtil.getById(id, repository);
    }

    @Override
    public Object delete(Long id) {
        return CrudUtil.delete(id, repository);
    }
    @Override
    public Object update(Dynamic dynamic) {

        Dynamic existData = repository.findById(dynamic.getId()).get();
        copyNonNullProperties(dynamic, existData);
        return CrudUtil.update(existData, repository);
    }

    @Override
    @SuppressWarnings("Duplicates")
    public Object create(Dynamic dynamic) {
        try {
            Dynamic createData = repository.save(dynamic);
            // 使用 uuid 作为源 token
            //String     token = UUID.randomUUID().toString().replace("-", "");
            //TokenModel model = new TokenModel(createData.getId(), token);
            // 30 分钟有效期
            //cacheManager.set(model.getToken(), new CacheModel(TOKEN_MINUTE, model));
            return Result.GetMapData(Result.CodeCreate, Result.MsgCreate);//, model);
        } catch (Exception e) {
            return Result.GetMapData(Result.CodeSql, e.getCause().getCause().getMessage());
        }
    }

    @Override
    public List<Map> list(){return repository.list();};

    @Override
    public List<Map> idlist(long id){return repository.idlist(id);};

    @Override
    public List<Map> getListByName(String name){return repository.getListByName(name);}

    @Override
    public List<Map> getListByTag(String circle_name,String dynamic_company_tag,String dynamic_major_tag,String dynamic_job_tag,String dynamic_other_tag) {
        return repository.getListByTag(circle_name,dynamic_company_tag, dynamic_major_tag, dynamic_job_tag, dynamic_other_tag);
    };
    @Override
    public List<Map> getListByTagNoCircle(String dynamic_company_tag,String dynamic_major_tag,String dynamic_job_tag,String dynamic_other_tag) {
        return repository.getListByTagNoCircle(dynamic_company_tag, dynamic_major_tag, dynamic_job_tag, dynamic_other_tag);
    }

    @Transactional
    @Override
    public Object updateDynamicForward(Long id) {

        try {
            repository.updateDynamicForward(id);
            return Result.GetMapData(Result.CodeSuccess,Result.MsgSuccess);//, model);
        } catch (Exception e) {
            return Result.GetMapData(Result.CodeSql, e.getCause().getCause().getMessage());
        }
    }
}
